<?php
include_once 'classes/DBconnect.php';
/**
 * Class mapping data users of DB 
 */
class DaoUsers
{
	private $dbConnect;
	
	public function __construct()
	{
		$this->dbConnect = DBconnect::getInstance();
	}
	
	/**
 	 * Return result query DB MySQL
 	 */
	public function getData($textquery)
	{
		$queryResult = $this->dbConnect->query($textquery);
		return $queryResult;
	}
	
	public function registerUser($post)
	{
		$date = $post['year']."-".$post['month']."-".$post['day'];
		$this->getData("INSERT INTO users (id, login, password, name, sex, dateofbirth)
						VALUES (NULL , '".$post['login']."','"
						.md5($post['password'][0])."','"
						.$post['name']."','"
						.$post['sex']."','"
						.$date."')");
		return true;
	}
	
	/**
	 * Name checking on the availability of
	 * 
	 * @param $login new login user
	 */
	public function freeLogin($login)
	{
		$a = $this->getData("SELECT id FROM users WHERE login LIKE '".$login."'");
		$b = mysql_num_rows($a);
		if(!empty($b))
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	/**
	 * Authorization users
	 * 
	 * @param $login - login user
	 * @param $password - password user
	 */
	public function loginUser($login, $password)
	{
		$res = $this->getData("SELECT * FROM users WHERE login='".$login."'
        								AND password='".$password."'");
		if (mysql_num_rows($res)!=1)
		{	
			return false;
		}
		else
		{	
			return true;
		}
	}
	
	/**
	 * Checking whether the user is logged in
	 */
	public function nologout()
	{	
		$res= $this->getData("SELECT * FROM users WHERE login='".$_SESSION['login']."'");
        if (mysql_num_rows($res)!=1)
        {    
        	return false;
        }
    	else
    	{   
    		return true;	
    	}
   	}
}